package com.time.workshop.ui;

import org.json.JSONObject;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.pingplusplus.android.PaymentActivity;
import com.time.workshop.API;
import com.time.workshop.BaseConstant;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.bean.BaseResponse;
import com.time.workshop.bean.OrderDetail;
import com.time.workshop.bean.OrderType;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.Log;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.PreferenceUtil;

/**
 * @author fanbaolong 订单详情
 */
public class OrderDetailActivity extends SuperActivity {

	@V
	ImageView img_login_back , weixin_true , zhifubao_true;
	@V
	private RelativeLayout order_content_Layout, order_remarks_Layout,
	order_keshi_Layout,order_zhuanjia_Layout,
	order_canting_address_Layout,order_address_Layout, order_mealtime_Layout,
	order_mealcount_Layout ,pay_Layout ,weixin_Layout ,zhifubao_Layout ,order_price_Layout,
	order_spare1_Layout , order_spare2_Layout,order_age_Layout;
	@V
	private LinearLayout ll_pay ;
	@V
	private TextView order_advanceTextView , order_address ,order_mealtime ,order_spare1 ,order_spare2,order_ageTextView;
	@V
	private View order_mealtimeview, order_mealcountview, order_keshiview,order_zhuanjiaview,
	order_canting_addressview,order_addressview , order_spare1view ,order_spare2view;
	@V
	private TextView order_priceTextView, order_mealtimeTextView,
	order_mealcountTextView, order_contentTextView, order_nameTextView,
	order_phoneTextView, order_canting_addressTextView,order_addressTextView,
	order_keshiTextView,order_zhuanjiaTextView,order_remarksTextView,
	pay_price,pay_commit , order_spare1TextView ,order_spare2TextView;

	private String orderId , pay_type;

	private class Response extends BaseResponse<OrderDetail> {
	}

	private Response mResponse;

	private OrderDetail mOrderDetail;

	private static final int REQUEST_CODE_PAYMENT = 1;
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_order_detail);
		init();
	}

	private void init() {
		Injector.getInstance().inject(this);
		img_login_back.setOnClickListener(this);
		pay_commit.setOnClickListener(this);
		weixin_Layout.setOnClickListener(this);
		zhifubao_Layout.setOnClickListener(this);
		orderId = getIntent().getStringExtra("orderId");

		initData();
	}

	private void initData() {
		if (!CommonUtils.isNetworkConnected(getApplicationContext())) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.M000));
			return;
		}
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("token",
				PreferenceUtil.getString(this, BaseConstant.PREF_TOKE_STRING));
		params.addQueryStringParameter("id", orderId);

		HttpUtils httpUtils = new HttpUtils();
		Log.d("requestUrl==", API.orderDetail);

		httpUtils.send(HttpMethod.GET, API.orderDetail, params,
				new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.d("onSuccess==", responseInfo.result);
				Gson gson = new Gson();

				mResponse = gson.fromJson(responseInfo.result,
						Response.class);

				if (mResponse.isSuccess()) {

					mOrderDetail = mResponse.getResult();

					setData();

				} else {
					NewToast.makeText(getApplicationContext(),
							mResponse.getMessage());
				}

			}

			@Override
			public void onFailure(HttpException error, String msg) {
				Log.d("onFailure==", msg);
				NewToast.makeText(getApplicationContext(), msg);
			}

		});

	}

	private void setData() {
		if ("3".equals(mOrderDetail.getSTATUS())) {
			pay_Layout.setVisibility(View.VISIBLE);
			ll_pay.setVisibility(View.VISIBLE);
		}else {
			pay_Layout.setVisibility(View.GONE);
			ll_pay.setVisibility(View.GONE);
		}
		
		//价钱
		if ("".equals(CommonUtils.isNull(mOrderDetail.getPRICE()))) {
			order_price_Layout.setVisibility(View.GONE);
		}else {
			order_priceTextView.setText("￥" + (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail.getPRICE()));
			double price = Double.valueOf(mOrderDetail.getPRICE());
			pay_price.setVisibility(View.VISIBLE);
			pay_price.setText("合计:"+price+"元");
		}

		order_advanceTextView.setText(mOrderDetail.getTITLE());
		order_nameTextView.setText(mOrderDetail.getCONTACTOR_PERSON());
		order_phoneTextView.setText(mOrderDetail.getCONTACTOR_PHONE());
		if (OrderType.YDDD.toString().equals(mOrderDetail.getTYPE_CODE())) {// 订座

			order_remarks_Layout.setVisibility(View.VISIBLE);
			order_mealcount_Layout.setVisibility(View.VISIBLE);
			order_mealcountview.setVisibility(View.VISIBLE);
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtimeview.setVisibility(View.VISIBLE);


//			order_priceTextView.setText("￥"+ (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail.getPRICE()));
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());
			order_mealcountTextView.setText(mOrderDetail
					.getRESERVATION_PERSON());
			order_remarksTextView.setText(mOrderDetail.getREMARK());
			
		} else if (OrderType.WMDD.toString().equals(mOrderDetail.getTYPE_CODE())) {// 外卖
			order_price_Layout.setVisibility(View.GONE);
			order_remarks_Layout.setVisibility(View.VISIBLE);
			order_mealcount_Layout.setVisibility(View.GONE);
			order_canting_address_Layout.setVisibility(View.VISIBLE);
			order_canting_addressview.setVisibility(View.VISIBLE);
			order_address_Layout.setVisibility(View.VISIBLE);
			order_mealcountview.setVisibility(View.VISIBLE);
			order_addressview.setVisibility(View.VISIBLE);
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtimeview.setVisibility(View.VISIBLE);

//			order_priceTextView.setText("￥"+ (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail.getPRICE()));
			order_mealcountTextView.setText(mOrderDetail
					.getRESERVATION_PERSON());
			order_canting_addressTextView.setText(mOrderDetail.getSTORE_ADDRESS());
			order_addressTextView.setText(mOrderDetail.getCONTACTOR_ADDRESS());
			order_remarksTextView.setText(mOrderDetail.getREMARK());
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());
			
			

		} else if (OrderType.TCDD.toString().equals(mOrderDetail.getTYPE_CODE())) {// 同城

			order_content_Layout.setVisibility(View.VISIBLE);
			order_price_Layout.setVisibility(View.GONE);
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtimeview.setVisibility(View.VISIBLE);
			//价钱
//			if ("".equals(CommonUtils.isNull(mOrderDetail.getPRICE()))) {
//				order_price_Layout.setVisibility(View.GONE);
//			}else {
//				order_priceTextView.setText("￥"
//						+ (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail
//								.getPRICE()));
//			}
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());
			order_contentTextView.setText(mOrderDetail.getREMARK());
			
		} else if (OrderType.GHDD.toString()
				.equals(mOrderDetail.getTYPE_CODE())) {// 挂号
			order_price_Layout.setVisibility(View.GONE);
			order_content_Layout.setVisibility(View.GONE);
			order_keshi_Layout.setVisibility(View.VISIBLE);
			order_zhuanjia_Layout.setVisibility(View.VISIBLE);
			order_keshiview.setVisibility(View.VISIBLE);
			order_zhuanjiaview.setVisibility(View.VISIBLE);
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtimeview.setVisibility(View.VISIBLE);
			
//			order_priceTextView.setText("￥"
//					+ (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail
//							.getPRICE()));
			order_contentTextView.setText(mOrderDetail.getREMARK());
			order_keshiTextView.setText(mOrderDetail.getDEPARTMENT_NAME());
			order_zhuanjiaTextView.setText(mOrderDetail.getPERSON_NAME());
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());
			
		} else if(OrderType.LRDS.toString().equals(mOrderDetail.getTYPE_CODE()) || OrderType.LRMC.toString().equals(mOrderDetail.getTYPE_CODE()) ){ 
			//老人买菜和老人打扫

			order_remarks_Layout.setVisibility(View.VISIBLE);
			order_mealcount_Layout.setVisibility(View.GONE);
			order_address_Layout.setVisibility(View.VISIBLE);
			order_addressview.setVisibility(View.VISIBLE);
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtimeview.setVisibility(View.VISIBLE);
			order_address.setText(getResources().getString(R.string.address));
			
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());
			//价钱
//			if ("".equals(CommonUtils.isNull(mOrderDetail.getPRICE()))) {
//				order_price_Layout.setVisibility(View.GONE);
//			}else {
//				order_priceTextView.setText("￥" + (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail.getPRICE()));
//			}
			order_addressTextView.setText(mOrderDetail.getCONTACTOR_ADDRESS());
			order_remarksTextView.setText(mOrderDetail.getREMARK());
		} else if(OrderType.LRPL.toString().equals(mOrderDetail.getTYPE_CODE()) ){ 
			//老人陪聊

			order_content_Layout.setVisibility(View.GONE);
			order_address_Layout.setVisibility(View.VISIBLE);
			order_addressview.setVisibility(View.VISIBLE);
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtimeview.setVisibility(View.VISIBLE);
			order_age_Layout.setVisibility(View.VISIBLE);
			order_address.setText(getResources().getString(R.string.address));
			
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());
			order_ageTextView.setText(mOrderDetail.getAGE());
			//价钱
//			if ("".equals(CommonUtils.isNull(mOrderDetail.getPRICE()))) {
//				order_price_Layout.setVisibility(View.GONE);
//			}else {
//				order_priceTextView.setText("￥" + (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail.getPRICE()));
//			}
			order_addressTextView.setText(mOrderDetail.getCONTACTOR_ADDRESS());

		} else if (OrderType.XYHZ.toString().equals(mOrderDetail.getTYPE_CODE())) {// 许愿

			//价钱
//			if ("".equals(CommonUtils.isNull(mOrderDetail.getPRICE()))) {
//				order_price_Layout.setVisibility(View.GONE);
//			}else {
//				order_priceTextView.setText("￥"
//						+ (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail
//								.getPRICE()));
//			}
			//内容
			order_content_Layout.setVisibility(View.VISIBLE);
			order_contentTextView.setText(mOrderDetail.getREMARK());
		} else if (OrderType.SJGF.toString().equals(mOrderDetail.getTYPE_CODE())) {//贩卖时间
			//价钱
//			if ("".equals(CommonUtils.isNull(mOrderDetail.getPRICE()))) {
//				order_price_Layout.setVisibility(View.GONE);
//			}else {
//				order_priceTextView.setText("￥"
//						+ (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail
//								.getPRICE()));
//			}
			//时间
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtime.setText(getResources().getString(R.string.time));
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());
			//地址
			order_address_Layout.setVisibility(View.VISIBLE);
			order_addressview.setVisibility(View.VISIBLE);
			order_address.setText(getResources().getString(R.string.address));
			order_addressTextView.setText(mOrderDetail.getCONTACTOR_ADDRESS());
			//备注
			order_remarks_Layout.setVisibility(View.VISIBLE);
			order_remarksTextView.setText(mOrderDetail.getREMARK());
		}else if (OrderType.LCWDD.toString().equals(
				mOrderDetail.getTYPE_CODE())) {//接送服务
//			if ("".equals(CommonUtils.isNull(mOrderDetail.getPRICE()))) {
//				order_price_Layout.setVisibility(View.GONE);
//			}else {
//				order_priceTextView.setText("￥"
//						+ (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail
//								.getPRICE()));
//			}
			//时间
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtime.setText(getResources().getString(R.string.time));
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());
			//备注
			order_remarks_Layout.setVisibility(View.VISIBLE);
			order_remarksTextView.setText(mOrderDetail.getREMARK());

			//设置为车型
			order_address_Layout.setVisibility(View.VISIBLE);
			order_addressview.setVisibility(View.VISIBLE);
			order_address.setText(getResources().getString(R.string.jiesong_cartype));
			order_addressTextView.setText(mOrderDetail.getCAR_NAME());

			//设置为接的地址
			order_spare1_Layout.setVisibility(View.VISIBLE);
			order_spare1view.setVisibility(View.VISIBLE);
			order_spare1.setText(getResources().getString(R.string.jie_address));
			order_spare1TextView.setText(mOrderDetail.getCONTACTOR_ADDRESS());

			//设置为送的地址
			order_spare2_Layout.setVisibility(View.VISIBLE);
			order_spare2view.setVisibility(View.VISIBLE);
			order_spare2.setText(getResources().getString(R.string.song_address));
			order_spare2TextView.setText(mOrderDetail.getSEND_ADDRESS());
		}else if ( OrderType.PPDD.toString().equals(mOrderDetail.getTYPE_CODE())) {//陪跑
//			if ("".equals(CommonUtils.isNull(mOrderDetail.getPRICE()))) {
//				order_price_Layout.setVisibility(View.GONE);
//			}else {
//				order_priceTextView.setText("￥"
//						+ (mOrderDetail.getPRICE() == null ? 0 : mOrderDetail
//								.getPRICE()));
//			}

			//时间
			order_mealtime_Layout.setVisibility(View.VISIBLE);
			order_mealtime.setText(getResources().getString(R.string.time));
			order_mealtimeTextView.setText(mOrderDetail.getRESERVATION_TIME());

			//地址
			order_address_Layout.setVisibility(View.VISIBLE);
			order_addressview.setVisibility(View.VISIBLE);
			order_address.setText(getResources().getString(R.string.address));
			order_addressTextView.setText(mOrderDetail.getCONTACTOR_ADDRESS());

			//备注
			order_remarks_Layout.setVisibility(View.VISIBLE);
			order_remarksTextView.setText(mOrderDetail.getREMARK());
		}

	}

	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.img_login_back:
			CommonUtils.hide(this);
			back();
			break;

		case R.id.weixin_Layout:
			weixin_true.setImageResource(R.drawable.btn_choose_pressed);
			zhifubao_true.setImageResource(R.drawable.btn_choose_normal);
			pay_type = "wx";
			break;
		case R.id.zhifubao_Layout:
			zhifubao_true.setImageResource(R.drawable.btn_choose_pressed);
			weixin_true.setImageResource(R.drawable.btn_choose_normal);
			pay_type = "alipay";
			break;
		case R.id.pay_commit:
			requestPay();
			break;
		default:
			break;
		}
	}

	private void requestPay(){
		if (!CommonUtils.isNetworkConnected(getApplicationContext())) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.M000));
			return;
		}
		if ("".equals(CommonUtils.isNull(pay_type))) {
			NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_paytype));
			return;
		}
		showLoadingDialog();
		pay_commit.setClickable(false);
		RequestParams param = new RequestParams();
		param.addBodyParameter("token", PreferenceUtil.getString(getApplicationContext(), BaseConstant.PREF_TOKE_STRING));
		param.addBodyParameter("id", mOrderDetail.getID());
		param.addBodyParameter("ORDER_TYPE", mOrderDetail.getTYPE_CODE());
		param.addBodyParameter("pay_type", pay_type);
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, API.pay, param, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.d("onSuccess==", responseInfo+"");
				pay_commit.setClickable(true);
				Intent in ;
				try {
					JSONObject object = new JSONObject(responseInfo.result);
					if (object.getBoolean("success")) {
						String result = object.getString("result");
						if (!"".equals(CommonUtils.isNull(result))) {
							in = new Intent();
							String packageName = getPackageName();
							ComponentName componentName = new ComponentName(packageName, packageName + ".wxapi.WXPayEntryActivity");
							in.setComponent(componentName);
							in.putExtra(PaymentActivity.EXTRA_CHARGE, result.toString());
							startActivityForResult(in, REQUEST_CODE_PAYMENT);
						}
					}else {
						if (1 == object.getInt("resCode")) {
							in = new Intent(OrderDetailActivity.this , LoginActivity.class);
							startActivity(in, true);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				Log.d("onFailure==", msg+"");
				NewToast.makeText(getApplicationContext(), "fail"+msg);
				pay_commit.setClickable(true);
			}
		});
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		pay_commit.setClickable(true);
		dismissLoadingDialog();
		//支付页面返回处理
		if (requestCode == REQUEST_CODE_PAYMENT) {
			if (resultCode == Activity.RESULT_OK) {
				String result = data.getExtras().getString("pay_result");
				/* 处理返回值
				 * "success" - payment succeed
				 * "fail"    - payment failed
				 * "cancel"  - user canceld
				 * "invalid" - payment plugin not installed
				 */
				String errorMsg = data.getExtras().getString("error_msg"); // 错误信息
				String extraMsg = data.getExtras().getString("extra_msg"); // 错误信息
				NewToast.makeText(getApplicationContext(), result + errorMsg + extraMsg);
				back();
			} else if (resultCode == Activity.RESULT_CANCELED) {
				NewToast.makeText(getApplicationContext(), "User canceled");
			}
		}
		
	}

}
